// pages/processor/processor.ts
import { orderProcessor } from '../../../api/order'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputOption: {
      maxHeight: 500,
      minHeight: 100
    },
    picUrls: [],
    isLoading: false,
    userSign: '',
    livingRoomTemperature: '',
    bedRoomTemperature: '',
    secondRommTemperature: '',
    insideWaterTemperature: '',
    outWaterTemperature: '',
    flow: '',
    repairWork: '',
    userEvaluation: '2',
    id: -1,
    content: [
      {
        title: '清网',
        content: '清网'
      },
      {
        title: '放气',
        content: '放气'
      },
      {
        title: '冲管',
        content: '冲管'
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    this.setData({
      id: options.id
    })
  },
  onClickLeft() {
    wx.navigateBack({
      delta: 1
    })
  },
  hasNoEmptyValues(obj: any) {
    for (let key in obj) {
      if (obj[key] === null || obj[key] === '' || obj[key] === undefined) {
        return false; // 如果找到空值，返回false
      }
    }
    return true; // 如果没有找到空值，返回true
  },
  async onSubmit() {
    const comID = wx.getStorageSync("userInfo").data.comID
    this.setData({
      isLoading: true
    })
    const {
      userSign,
      picUrls,
      livingRoomTemperature,
      bedRoomTemperature,
      secondRommTemperature,
      insideWaterTemperature,
      outWaterTemperature,
      flow,
      repairWork,
      userEvaluation } = this.data
    let data;
    if (comID == 14) {
      data = {
        // livingRoomTemperature: livingRoomTemperature,
        // bedRoomTemperature: bedRoomTemperature,
        // secondRommTemperature: secondRommTemperature,
        // insideWaterTemperature: insideWaterTemperature,
        // outWaterTemperature: outWaterTemperature,
        // flow: flow,
        repairWork: repairWork,
        userEvaluation: userEvaluation
      }
    } else {
      data = {
        livingRoomTemperature: livingRoomTemperature,
        bedRoomTemperature: bedRoomTemperature,
        secondRommTemperature: secondRommTemperature,
        insideWaterTemperature: insideWaterTemperature,
        outWaterTemperature: outWaterTemperature,
        flow: flow,
        repairWork: repairWork,
        userEvaluation: userEvaluation
      }
    }

    const isNull = this.hasNoEmptyValues(data)
    let imgeUrl = ''
    console.log(isNull)
    if (comID == 14) {
      if (picUrls.length > 0 && isNull) {
        // console.log('通过校验')
        for (let i = 0; picUrls.length > i; i++) {
          const res = await this.upload(picUrls[i].url)
          imgeUrl += JSON.parse(res.data).msg + ';'
          // console.log(imgeUrl)
        }
        // const res = await this.upload(userSign)
        // this.setData({
        //   userSign: JSON.parse(res.data).msg
        // })
  
        await orderProcessor(comID, {
          ...data,
          userSign: this.data.userSign,
          picUrls: imgeUrl,
          requestid: this.data.id
        }).then((res: any) => {
          console.log(res)
          if (res.data.code == 200) {
            this.setData({
              isLoading: false
            })
            wx.showToast({
              title: '提交成功',
              icon: 'success',
              success: () => {
                wx.navigateBack({
                  delta: 2
                })
              }
            })
          } else {
            this.setData({
              isLoading: false
            })
            wx.showToast({
              title: '提交失败',
              icon: 'error'
            })
          }
        }).catch((err: any) => {
          this.setData({
            isLoading: false
          })
          wx.showToast({
            title: '提交失败',
            icon: 'error'
          })
        })
        // console.log('校验通过', userSign)
      } else {
        wx.showToast({
          title: '缺少必填项',
          icon: 'error'
        })
        // console.log('校验失败', userSign)
      }
    } else {
      if (userSign && picUrls.length > 0 && isNull) {
        for (let i = 0; picUrls.length > i; i++) {
          const res = await this.upload(picUrls[i].url)
          imgeUrl += JSON.parse(res.data).msg + ';'
          // console.log(imgeUrl)
        }
        const res = await this.upload(userSign)
        this.setData({
          userSign: JSON.parse(res.data).msg
        })

        await orderProcessor(comID, {
          ...data,
          userSign: this.data.userSign,
          picUrls: imgeUrl,
          requestid: this.data.id
        }).then((res: any) => {
          console.log(res)
          if (res.data.code == 200) {
            this.setData({
              isLoading: false
            })
            wx.showToast({
              title: '提交成功',
              icon: 'success',
              success: () => {
                wx.navigateBack({
                  delta: 2
                })
              }
            })
          } else {
            this.setData({
              isLoading: false
            })
            wx.showToast({
              title: '提交失败',
              icon: 'error'
            })
          }
        }).catch((err: any) => {
          this.setData({
            isLoading: false
          })
          wx.showToast({
            title: '提交失败',
            icon: 'error'
          })
        })
        // console.log('校验通过', userSign)
      } else {
        wx.showToast({
          title: '缺少必填项',
          icon: 'error'
        })
        // console.log('校验失败', userSign)
      }
    }
  },
  afterRead(event: any) {
    const { file } = event.detail;
    const _this = this
    const { picUrls = [] } = _this.data;
    picUrls.push({ ...file, url: file.url });
    _this.setData({ picUrls });
    console.log(file.url)
  },
  deleteImg(e: any) {
    const index: number = e.detail.index
    this.data.picUrls.splice([index], 1)
    this.setData({
      fileList: this.data.picUrls
    });
    let img_list = [];
    this.data.picUrls.forEach((e: any) => {
      img_list.push(e.url)
    });
  },
  upload(file: string) {
    return new Promise((resolve, reject) => {
      wx.uploadFile({
        // url: 'http://192.168.2.48:8080/MobileLinkCenter/picture/pictureUpload', // 仅为示例，非真实的接口地址
        url: 'https://www.hbdinkon.cn:10020/MobileLinkCenter/picture/pictureUpload',
        filePath: file,
        name: 'file',
        success(res) {
          resolve(res)
        },
        fail(error) {
          reject(error);
        },
      });
    })
  },
  onContent(e: any) {
    this.setData({
      repairWork: e.currentTarget.dataset.content
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})